package com.example.manager.mapper;

import com.example.manager.entity.Book;
import com.example.manager.entity.Borrow;
import com.example.manager.entity.Follow;
import com.example.manager.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookMapper {
    @Select("insert into book(bookname, author, publisher,pubdate,pressmark,language,ISBN,price,summary,status,typeId,img) values (#{bookname},#{author},#{publisher},#{pubdate},#{pressmark},#{language},#{isbn},#{price},#{summary},#{status},#{typeId},#{img})")
    void insert(Book book);

    @Select("select * from book where bookId = #{bookid}")
    Book findById(int bookid);


    @Select("select count(*) from book ")
    Integer getAllBookSize();

    @Select("select * from book limit #{currentpage}, #{pageSize}")
    List<Book> getBookByPage(@Param("currentpage") Integer currentpage, @Param("pageSize") Integer pageSize);

    @Select("delete from book where bookId=#{bookid}")
    void delBook(@Param("bookid") Integer bookid);

    @Update("update book set bookName=#{bookname}, author=#{author}, publisher=#{publisher}, price=#{price}, summary=#{summary}, status=#{status}, typeId=#{typeId}, ISBN=#{isbn}, language=#{language}, pressmark=#{pressmark}, pubdate=#{pubdate} where bookId=#{bookid}")
    void updatebook(Book book);

    @Select("select count(*) from book where bookName like '%${search}%' or author like '%${search}%' or publisher like '%${search}%'")
    Integer getAllBookSize1(String search);

    @Select("select * from book where bookname like '%${search}%' or author like '%${search}%' or publisher like '%${search}%'")
    List<Book> findBook(String search);

    @Update("update book set status=1 where bookId=#{bookid}")
    void borrowBook(Borrow borrow);

    @Insert("insert into borrow(bookid, userid) values(#{bookid},#{userid})")
    void insertborrow(Borrow borrow);

    @Select("select count(*) from book as e join borrow b on e.bookId = b.bookid where b.userid = #{username}")
    Integer getAllBorrowSize(Integer username);

    @Select("select a.*,b.* from book as a join borrow b on a.bookId = b.bookid where b.userid=#{username} limit #{currentpage}, #{pageSize}")
    List<Book> getBorrowBooks(Integer username, @Param("currentpage") Integer currentpage, @Param("pageSize") Integer pageSize);

    @Select("delete from borrow where bookid=#{bookid} ")
    public String backbook(Integer bookid);

    @Update("update book set status=0 where bookId=#{bookid}")
    void backbook1(Integer bookid);

    @Select("select count(*) from book as e join borrow b on e.bookId = b.bookid")
    Integer getAllBorrowSize1();

    @Select("select a.*,b.* from book as a join borrow b on a.bookId = b.bookid limit #{currentpage}, #{pageSize}")
    List<Book> getBorrowBooks1( @Param("currentpage") Integer currentpage, @Param("pageSize") Integer pageSize);


    @Insert("insert into follow(bookid,userid) values (#{bookid},#{userid})")
    void followBook(Follow follow);

    @Select("delete from follow where bookid=#{bookid} ")
    public String delFollowBook(Integer bookid);

    @Select("select a.*,b.* from book as a join follow b on a.bookId = b.bookid where b.userid=#{username} limit #{currentpage}, #{pageSize}")
    List<Book> getFollowBooks(Integer username, @Param("currentpage") Integer currentpage, @Param("pageSize") Integer pageSize);

    @Select("select count(*) from book as e join follow b on e.bookId = b.bookid where b.userid = #{username}")
    Integer getAllFollowSize(Integer username);

    @Select("select a.bookId from book as a join follow b on a.bookId = b.bookid where b.userid=#{username}")
    List<Integer> checkFollowBook(Integer username);
}
